"""
    URLS API
    @author: Jean-Lou Dupont
"""

import os
import sys
import logging
import string

import wsgiref.handlers
from google.appengine.ext           import webapp
from google.appengine.ext.webapp    import template
from google.appengine.api           import users

class UrlsApiHandler(webapp.RequestHandler):
    
    def get(self, dest):
        user = users.get_current_user()
        login_url = users.create_login_url(dest)
        logout_url = users.create_logout_url(dest)        

        template_values = { 'login_url': login_url, 'logout_url': logout_url };
        
        try:
            path = os.path.join(os.path.dirname(__file__), 'templates/urls.json')  #TODO hardened...  
            self.response.out.write(template.render(path, template_values))
            self.response.headers["Content-Type"] = "application/json"
        except:
            self.response.set_status( 404 )
        
#========================================================================================================
def main():
  application = webapp.WSGIApplication([('/api/urls/(.*)/',UrlsApiHandler) ], debug=True)
  wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
    main()        